﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BaMusaSoft.RPS.DataModel;
using BaMusaSoft.RPS.DataModel.Repositories;
using BaMusaSoft.RPS.ViewModel.Infrastructure;

namespace BaMusaSoft.RPS.ViewModel
{
    public class AddContractsClaimedViewModel:Workspace
    {
        IUnitOfWork _unitOfWork;
        Customer _claimedCustomer;
        ContractsRepository _contractsRepository;
        ObservableCollection<Contract> _contracts;
        IList<Contract> _selectedContracts;
        public AddContractsClaimedViewModel(IUnitOfWork unitOfWork, Customer claimedCustomer)
        {
            if (unitOfWork == null) throw new ArgumentNullException("unitOfWork");
            if (claimedCustomer == null) throw new ArgumentNullException("calimedCustomer");
            _unitOfWork = unitOfWork;
            _claimedCustomer = claimedCustomer;
            _contractsRepository = _unitOfWork.Contracts as ContractsRepository;
            Contracts = GetData();
            SelectedContracts = new List<Contract>();

        }

        private ObservableCollection<Contract> GetData()
        {
            var customerContracts = _contractsRepository.Query( contract =>
                contract.Closed != true &&
                contract.Customer.CustomerId == _claimedCustomer.CustomerId );
            return new ObservableCollection<Contract>(customerContracts);
        }
        public ObservableCollection<Contract> Contracts
        {
            get { return _contracts; }
            private set
            {
                _contracts = value;
                RaisePropertyChanged("Contracts");

            }
        }
        public IList<Contract> SelectedContracts
        {
            get { return _selectedContracts; }
            private set
            {
                _selectedContracts = value;
            }
        }
        public void AddToSelectedContract(Contract contract)
        {
            if (contract == null) throw new ArgumentNullException("contract");
            SelectedContracts.Add(contract);
        }
        public void RemoveFromSelectedContracts(Contract contract)
        {
            if (contract == null) throw new ArgumentNullException("contract");
            SelectedContracts.Remove(contract);
        }

        #region "Base"
        public override string DisplayName
        {
            get
            {
                throw new NotImplementedException();
            }
            protected set
            {
                throw new NotImplementedException();
            }
        }

        public override bool HasChanges
        {
            get { throw new NotImplementedException(); }
        }
        #endregion
    }
}
